package br.com.componentes;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;

import br.com.image.ControleImagem;
import br.com.interfaces.ISprite;

public class Sprite implements ISprite
{
	/**
	 * Coordenadas de Destino
	 */
	private Point destino1 = new Point( 0, 0 );
	private Point destino2 = new Point( 0, 0 );
	
	/**
	 * Coordenadas da Imagem
	 */
	private Point source1 = new Point( 0, 0 );
	private Point source2 = new Point( 0, 0 );
	
	private Dimension size = new Dimension( );
	
	private boolean isAnimado;
	private boolean autoAnimado;
	private boolean isSeMovendo;
	
	private int frameY = 0;
	
	private int frameUp;
	private int frameDown;
	private int frameLeft;
	private int frameRight;
	
	private BufferedImage image;
	
	private long lastTimeAnimation = 0;
	private int  animationTime     = 100;
	
	public Sprite( String image, int CountVertic, int countHorizont )
	{
		this.image = ControleImagem.carregarSprite( image );
		
						        this.destino1.x = this.source1.x = 0;
					            this.destino1.y = this.source1.y = 0;
		this.getSize( ).width  = this.destino2.x = this.source2.x = ( this.getImage().getWidth( )  / CountVertic   );
		this.getSize( ).height = this.destino2.y = this.source2.y = ( this.getImage().getHeight( ) / countHorizont );
		
		
		
		this.autoAnimado = true;
		this.isAnimado   = false;
		this.setSeMovendo(false);
		
		setFrameUp   ( 2 );
		setFrameDown ( 0 );
		setFrameLeft ( 1 );
		setFrameRight( 3 );
	}
	
	public void update( long time )
	{
		animation( time );
	}

	public void draw( Graphics2D g2d )
	{
		Graphics2D g = (Graphics2D)g2d.create( );	
		
		g.drawImage( getImage(), destino1.x, destino1.y, destino2.x, destino2.y, 
							source1.x,  source1.y,  source2.x,  source2.y, null );
		
		g.dispose( );
	}
	
	private void animation( long time )
	{
		lastTimeAnimation += time;
		
		if( lastTimeAnimation > animationTime )
		{
			if( isAnimado )
			{
				if( autoAnimado )
				{
					if( source1.x < ( getImage().getWidth( ) - getSize().width ) )
					{
						source1.x += getSize().width;
						source2.x += getSize().width;
					}
					else
					{
						source1.x = 0;
						source2.x = getSize().width;
						
						if( source1.y < ( getImage().getHeight( ) - getSize().height ) )
						{
							source1.y += getSize().height;
							source2.y += getSize().height;
						}
						else
						{
							source1.y = 0;
							source2.y = getSize().height;
						}
					}
				}
				else if( isSeMovendo() )
				{			
					source1.y = getSize().height * frameY;
					source2.y = getSize().height + (getSize().height * frameY);
					
					if( source1.x < ( getImage().getWidth( ) - getSize().width ) )
					{
						source1.x += getSize().width;
						source2.x += getSize().width;
					}
					else
					{
						source1.x = 0;
						source2.x = getSize().width;
					}
				}
			}
			
			lastTimeAnimation = 0;
		}
	}
	
	/**
	 * @param count
	 * @param direcao
	 */
	public void move( int count, int direcao ) 
	{ 
		if( direcao == getFrameUp( ) )
			moveUp( count );
		if( direcao == getFrameDown( ) )
			moveDown( count );
		if( direcao == getFrameRight( ) )
			moveRight( count );
		if( direcao == getFrameLeft( ) )
			moveLeft( count );
	}
	
	public void moveUp( int count ) 
	{
		setFrameY( getFrameUp( ) );
		this.destino1.y -= count;
		this.destino2.y -= count;
	}
	
	public void moveDown( int count ) 
	{
		setFrameY( getFrameDown( ) );
		this.destino1.y += count;
		this.destino2.y += count;
	}
	
	public void moveLeft( int count ) 
	{
		setFrameY( getFrameLeft( ) );
		this.destino1.x -= count;
		this.destino2.x -= count;
	}
	
	public void moveRight( int count ) 
	{
		setFrameY( getFrameRight( ) );
		this.destino1.x += count;
		this.destino2.x += count;
	}

	public void setLocation( Point location )
	{
		destino1 = location;
		destino2.x = getSize().width  + location.x;
		destino2.y = getSize().height + location.y;
	}
	
	public Point getLocation(  )
	{
		return destino1;
	}
	
	public boolean isAutoAnimation( )
	{
		return autoAnimado;
	}

	public void setAutoAnimado( boolean autoAnimation )
	{
		this.autoAnimado = autoAnimation;
	}
	
	public int getFrameY( )
	{
		return frameY;
	}
	
	public void setFrameY( int line )
	{
		this.frameY = line;
	}

	public boolean isAnimado( )
	{
		return isAnimado;
	}

	public void setAnimado( boolean isAnimado ) 
	{
		this.isAnimado = isAnimado;
	}

	public int getAnimationTime( )
	{
		return animationTime;
	}

	public void setAnimationTime( int animationTime ) 
	{
		this.animationTime = animationTime;
	}

	public BufferedImage getImage( ) 
	{
		return image;
	}

	public boolean isSeMovendo( ) 
	{
		return isSeMovendo;
	}

	public void setSeMovendo( boolean isSeMovendo )
	{
		this.isSeMovendo = isSeMovendo;
	}
	
	public void resetAnimationline( ) 
	{
		source1.x = 0;
		source2.x = getSize( ).width;
	}

	public int getFrameUp( )
	{
		return frameUp;
	}

	public void setFrameUp( int frameUp ) 
	{
		this.frameUp = frameUp;
	}

	public int getFrameDown( ) 
	{
		return frameDown;
	}

	public void setFrameDown( int frameDown )
	{
		this.frameDown = frameDown;
	}

	public int getFrameLeft( ) 
	{
		return frameLeft;
	}

	public void setFrameLeft( int frameLeft ) 
	{
		this.frameLeft = frameLeft;
	}

	public int getFrameRight( ) 
	{
		return frameRight;
	}

	public void setFrameRight( int frameRight )
	{
		this.frameRight = frameRight;
	}

	public Dimension getSize( )
	{
		return size;
	}

	public void setSize( Dimension size )
	{
		this.size = size;
	}

	public int getInvertDirecao( )
	{
		if( getFrameY( ) == getFrameUp( ) )
			return getFrameDown( );
		else if( getFrameY( ) == getFrameDown( ) )
			return getFrameUp( );
		else if( getFrameY( ) == getFrameLeft( ) )
			return getFrameRight( );
		else
			return getFrameLeft( );
	}
}